/*
 * Copyright (c) 2018-2024 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <strings.h>
#include <stdlib.h>
#include <getopt.h>

#include "echkpnt.lib.h"

static void restart_usage(const char *);

static char logMesgBuf[PATH_MAX+1];

int 
main(int argc, char **argv)
{
    char  *pMethodName = NULL;
    char  *pIsOutput = NULL;
    char  *pChkpntDir = NULL;
    char  erestartProgPath[PATH_MAX];      		
    char *cargv[MAX_ARGS];

    while (TRUE) {
        int option_index = 0;
        static struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"version", no_argument, 0, 'V'},
            {"method", required_argument, 0, 'm'},
            {NULL, 0, 0, 0}
        };

        int opt;
        opt = getopt_long(argc, argv, "hVm:", long_options, &option_index);
        if (-1 == opt) {
            break;
        }

        switch (opt) {
        case 'h':
            restart_usage(argv[0]);
            exit(0);
        case 'V':
            fputs(_QP_VERSION_, stdout);
            exit(0);
        case 'm':
            pMethodName = optarg;
            break;
        case '?':
        default:
            restart_usage(argv[0]);
            exit(1);
        }
    }

    if (argc == optind + 1) {
        pChkpntDir = argv[optind];
    } else {
        restart_usage(argv[0]);
        exit(-1);
    }

    if (NULL == pMethodName) {
        pMethodName = ECHKPNT_DEFAULT_METHOD;
    }

    if (access(pChkpntDir,W_OK | X_OK) < 0) {
        fprintf(stderr,"%s : the chkpnt dir %s can not be accessed\n",
                __func__, pChkpntDir);
        exit(-1);
    }

    if ((pIsOutput != NULL) && ((strcmp(pIsOutput,ECHKPNT_OPEN_OUTPUT) == 0)
                                ||(strcmp(pIsOutput,ECHKPNT_OPEN_OUTPUT_L) == 0))) {
        initLog((char *)"erestart");
    }

    if (getEchkpntMethodDir(erestartProgPath, ERESTART_PROGRAM, pMethodName) == NULL) {
        snprintf(logMesgBuf, PATH_MAX, "%s: the erestart method(%s) path is not correct\n",
                 __func__, pMethodName);
        goto Error;
    }

    int cargc;
    cargc = 0;
    cargv[cargc++] = strrchr(erestartProgPath, '/');

    if (argc > MAX_ARGS) {
        argc = MAX_ARGS;
    }

    int index;
    for (index=1; index<argc; index++) {
        if (0 == strcmp(argv[index], "-m") || 0 == strcmp(argv[index], "--method")) {
            index ++;
            continue;
        }

        cargv[cargc++] = argv[index];
    }
    cargv[argc] = NULL;

    closeLog();

    execv(erestartProgPath, cargv);
    snprintf(logMesgBuf, PATH_MAX, "%s: execute the erestart.default fail due to %m", __func__); 
    fprintf(stderr,"%s", logMesgBuf);
    exit(-1);

Error:

    fprintf(stderr,"%s",logMesgBuf);
    logMesg(logMesgBuf);
    closeLog();
    exit(-1);
}

void 
restart_usage(const char *cmd)
{
    fprintf(stdout, "%s usage: [-h] [-V] -m,--method=xxx chkdir\n", cmd);
    return;
}
